<template>
  <div>
    <div class="pageheader">
      <XHeader :left-options="{backText:'', preventGoBack: true}" :title="xheader.title" class="XHeader"
               @on-click-back="goBack"></XHeader>
    </div>

    <div class="pagetop">
      <div class="topbar block">
        <!--        <form @submit="onSoso($event)" class="inputbox flex flex-center">-->
        <form class="inputbox flex flex-center">
          <chimage class="icon" mode="aspectFit" :src="require('@/assets/img/ic_search.png')"></chimage>
          <input
            type="search"
            focus="true"
            style="text-align: left"
            v-model="goods_name"
            class="input flex-full"
            placeholder="搜索商品"
            @keyup.enter="onSoso()"
          >
          <div class="searchSubmit" @click="onSoso()">搜索</div>
        </form>
      </div>
    </div>

    <!--筛选排序-->
    <screen-item v-model="searchParams" @onSoso="onSoso" v-if="isScreen" :cateGoodsCount="cateGoodsCount"
                 :supplierList="supplierList"/>

    <div>
      <div style="margin-top: 12px" class="swiper-block" v-if="bannerList.length">
        <swiper class="block swiper-container-banner" :options="swiperOptionBanner" style="padding: 0 12px">
          <swiper-slide v-for="(item) in bannerList" :key="item.id">
            <img :src="item">
          </swiper-slide>
        </swiper>
        <div class="swiper-pagination" id="swiper-pagination-banner"></div>
      </div>

      <scroller
        @on-pullup-loading="getSpecialInfo(searchParams)"
        lock-x
        ref="scroller"
        :pullup-config="{content:'',upContent:'',downContent:'释放加载更多',loadingContent:'加载中...',}"
        height="-45"
        :use-pullup="true"
        style=" padding-bottom:72px;"
        v-model="status"
      >
        <div class="block page" style="margin-top: 12px;padding-bottom: 12px">
          <!--视频-->
          <video-playback
            style="background: none;margin-bottom: 10px;max-height: 230px"
            height="auto"
            v-if="topicDetail.video_url"
            :isImg="false"
            :imageUrl="topicDetail.image_url"
            :videoUrl="topicDetail.video_url"/>

          <!--商品列表-->
          <div class="grid grid-gutter specialbox">
            <div v-for="(item,index) in goodsList" :key="index" class="grid-1-2">
              <div class="item" :class="[isHasGoodsText(item) ? 'noStock' : '']" @click="onItemTap(item)">
                <div class="noStock-back" v-if="isHasGoodsText(item)"></div>
                <div class="noStock-con">
                  <img :src="item.goods_image_main">
                  <div class="noStock-tip" v-if="isHasGoodsText(item)">
                    <div class="noStock-tip-text">无货</div>
                  </div>
                </div>
                <div class="title">{{item.goods_name}}</div>
                <!--              <div class="pricebar">-->
                <!--                <div class="price">{{item.internal_price}}</div>-->
                <!--                <div class="d-price">{{item.reference_price}}</div>-->
                <!--              </div>-->
                <div class="list-price">
                  <div class="price newPrice">{{textNewPrice}}￥{{item.internal_price}}</div>
                  <!--<div v-if="discountClass(item.discount_rate)" class="discount" :class="discountClass(item.discount_rate)">-->
                  <!--<chimage class="icon" :src="require('@/assets/img/icon_discount.png')"></chimage>-->
                  <!--<span>{{item.discount_rate}}%</span>-->
                  <!--</div>-->
                  <div v-if="!isHasGoodsText(item)" class="shop-btn"
                       @click.stop="onAddToCart(item.goods_sku, item.goods_moq)">
                    <img :src="require('@/assets/img/icon_item_gwc.png')"/>
                  </div>
                </div>

                <div class="list-price">
                  <div class="price oldPrice">{{textOldPrice(item.supplier_id)}}￥{{item.reference_price}}</div>
                </div>

                <div class="list-price">
                  <div v-if="discountClass(item.discount_rate)" class="discount"
                       :class="discountClass(item.discount_rate)">
                    <chimage class="icon" :src="require('@/assets/img/icon_discount.png')"></chimage>
                    <span>{{item.discount_rate}}%</span>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>

        <load-more
          v-if="!more&&!goodsList.length"
          :show-loading="false"
          :tip="'暂无数据'"
          background-color="#fbf9fe"
        ></load-more>
      </scroller>
    </div>
  </div>
</template>

<script>
  import Mall from '../../../dll/mall'
  // import Swiper from 'swiper'
  // import 'swiper/dist/css/swiper.css'
  import { swiper, swiperSlide } from 'vue-awesome-swiper'
  import 'swiper/dist/css/swiper.css'
  import shop from '@/mixin/shop'
  import ScreenItem from '../../../components/screen'
  import VideoPlayback from '../../../components/videoItem/videoPlayback'

  export default {
    name: 'special',
    mixins: [shop],
    components: {
      swiper,
      swiperSlide,
      ScreenItem,
      VideoPlayback
    },
    data: () => {
      return {
        xheader: {
          title: '专题活动'
        },
        item: {},
        goodsList: [],
        bannerList: [],
        limit: 500,
        swiperOptionBanner: {
          spaceBetween: 12,
          autoplay: 2000,
          centeredSlides: true,
          disableOnInteraction: false,
          pagination: {
            el: '.swiper-pagination'
          }
        },

        // 筛选
        cateGoodsCount: [],
        supplierList: [],
        isScreen: false,
        searchParams: {},
        goods_name: '',
        more: true,
        so: {
          limit: 20,
          page: 1
        },
        status: {
          pullupStatus: 'default'
        },

        isVideo: undefined, // 是否试视频带货
        topicDetail: ''// 视频
      }
    },
    created () {
      this.item = this.$route.query.item ? JSON.parse(this.$route.query.item) : { topic_id: this.$route.params.topicId }
      this.isVideo = this.$route.query.isVideo
      this.getSpecialInfo()
    },
    watch: {
      // defaultAddress (val, oldVal) {
      //   if (oldVal && oldVal !== val) {
      //     this.getSpecialInfo(this.searchParams)
      //   }
      // }
    },
    methods: {
      onItemTap (item) {
        this.$router.push({ name: 'mallInfo', params: { id: item.goods_sku, item: item } })
      },

      onSoso (searchParams = {}) {
        this.so.page = 1
        this.more = true
        this.goodsList = []
        this.status.pullupStatus = 'disabled'
        this.$nextTick(function () {
          this.$refs.scroller.enablePullup()
          this.$refs.scroller.reset({
            top: 0
          })
          this.status.pullupStatus = 'default'
        })
        this.getSpecialInfo(searchParams)
      },

      // 获取专题商品列表
      async getSpecialInfo (searchParams = {}) {
        this.more = true
        this.$vux.loading.show({
          text: '加载中'
        })
        let param = {}
        param = Object.assign({}, param, this.so)
        if (!this.more) {
          return
        }
        // param.topic_id = this.$route.params.topicId
        param.topic_id = this.item.topic_id
        // eslint-disable-next-line no-unused-expressions
        this.goods_name ? param.keyword = this.goods_name : undefined
        param = Object.assign({
          sort_type: 'discount',
          sort_desc: 1
        }, param, searchParams, { area_code: this.defaultAddress.code })
        let server = this.isVideo ? Mall.getTopicVideoDetail : Mall.getTopicDetail
        let res = await server(param)
        this.item.topic_id = this.$route.params.topicId
        this.$vux.loading.hide()
        if (res.code == 0) {
          this.isScreen = true
          this.cateGoodsCount = res.data.cate_goods_count
          this.supplierList = res.data.supplier_list
          if (param.page >= res.data.page_total) {
            this.more = false
            this.$refs.scroller.disablePullup()
          } else {
            this.$refs.scroller.donePullup()
          }

          let goodsList = [].concat(this.goodsList, res.data.goods_list)
          this.goodsList = goodsList.filter((item) => { // 过滤掉无货商品
            return parseFloat(item.stock_state) !== -1
          })
          // this.xheader.title = res.data.title
          // this.goodsList = res.data.goods_list

          this.so.page = this.so.page + 1
          if (this.isVideo) {
            this.topicDetail = res.data.topic_detail
          } else {
            this.bannerList = res.data.banner_list
          }
          if (!this.goodsList.length) {
            this.more = false
          }
        } else {
          this.more = false
          this.$refs.scroller.disablePullup()
          this.$vux.confirm.show({
            title: '提示',
            content: res.msg,
            showCancelButton: false,
            onConfirm: () => {
              this.$router.back()
            }
          })
        }
      },

      // 返回首页
      goBack () {
        // this.$router.push({ name: 'mall' })
        if (this.isCQDX) {
          this.$router.push({ name: 'mall' })
        } else {
          this.$router.go(-1)
        }
      }
    }
  }
</script>

<style lang="less" scoped>
  .swiper {
    height: 206px;
    background-color: #fff;
  }

  .slide-image {
    height: 206px;
    width: 100%;
  }

  .specialbox {
    .item {
      display: block;
      background-color: #fff;
      padding: 12px;
      box-sizing: border-box;
    }

    .item {
      .noStock-con {
        img {
          display: block;
          height: 145px;
          width: 100%;
          background-color: #fbf8f7;
        }
      }

      > .title {
        margin-top: 16px;
        font-size: 12px;
        color: #575757;
        line-height: 17px;
        height: 34px;
        text-overflow: ellipsis;
        overflow: hidden;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
      }

      > .pricebar {
        display: flex;
        align-items: center;
      }

      .price {
        flex: 1;
        font-size: 14px;
        color: #ae2309;
        line-height: 16px;
      }

      .d-price {
        font-size: 10px;
        color: #d1c8c6;
        line-height: 11px;
        text-decoration: line-through;
      }
    }
  }

  .page {
    padding-top: 84px;
  }

  /deep/ .pageheader {
    z-index: 2 !important;
  }

  .pagetop {
    display: block;
    width: 100%;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 2;
  }

  .topbar {
    box-sizing: border-box;
    background-color: #fff;
    padding-top: 5px;
    padding-bottom: 5px;
  }

  .topbar .icon {
    width: 20px;
    height: 20px;
    padding: 0 5px;
    display: block;
  }

  .topbar .inputbox {
    background: #fbf8f7;
    height: 35px;
  }

  .searchSubmit {
    height: 35px;
    line-height: 35px;
    font-size: 12px;
    border: none;
    background-color: transparent;
    color: #351009;
    width: 60px;
    text-align: center;
  }

  .topbar .input {
    font-size: 12px;
    color: #351009;
    text-align: center;
    line-height: 17px;
  }

 /deep/ .videoPlayback .videoPlayback-con {
   object-fit: contain !important;
 }

</style>
